package org.example.quartz.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.example.quartz.service.LogService;
import org.example.quartz.service.OrgService;
import org.example.quartz.service.UtilService;
import org.example.quartz.util.SafeStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service("logServiceImplSlave1")
public class LogServiceImpl implements LogService {
    @Value(value = "${spring.datasource.databaseType}")
    private String databaseType;

    private final UtilService utilService;
    //orgService
    OrgService orgService = SpringUtil.getBean(OrgService.class);

    public LogServiceImpl(UtilService utilService) {
        this.utilService = utilService;
    }

    /**
     * 添加日志
     *
     * @param
     * @throws
     */
    @Override
    public int loginfo(JdbcTemplate jdbcTemplate, String methodName, String ts, String state, String inJson, String outJson, String dateid, String yyid, String gsUrl, String TS) {
        //字符串转义
        inJson = SafeStringUtils.returnValuetransfer(inJson);
        outJson = SafeStringUtils.returnValuetransfer(outJson);
        //SQL拼接
        String logInsert = "";
        //oracle需要使用TO_CLOB
        if (databaseType.equals("oracle")) {
            String toClob = "";
            String[] injson = strToStrArray(inJson, 2000);
            for (int i = 0; i < injson.length; i++) {
                toClob += "TO_CLOB('" + injson[i] + "')||";
            }
            toClob += "TO_CLOB('')";
            String toClob1 = "";
            String[] outjson = strToStrArray(outJson, 2000);
            for (int i = 0; i < outjson.length; i++) {
                toClob1 += "TO_CLOB('" + outjson[i] + "')||";
            }
            toClob1 += "TO_CLOB('')";

            String yyidStr = "";
            if (StrUtil.isNotBlank(yyid)) {
                String[] yyidjson = strToStrArray(yyid, 2000);
                for (int i = 0; i < yyidjson.length; i++) {
                    yyidStr += "TO_CLOB('" + yyidjson[i] + "')||";
                }
                yyidStr += "TO_CLOB('')";
            }
            logInsert = "INSERT INTO " + utilService.getGSYY() + ".gs_yy_log(ID,methodName,\n" +
                    " ts, state, inJson, outJson, dateid, yyid, gsUrl ,yyts) VALUES " +
                    "(SYS_GUID(),'" + methodName + "',SYSDATE,'" + state + "'," + toClob + "," + toClob1 + "," +
                    "'" + dateid + "'," +
                    "" + yyidStr + "," +
                    "'" + gsUrl + "'," +
                    "to_date('" + TS + "', 'yyyy-mm-dd hh24:mi:ss'))";

            if (StrUtil.isBlank(yyid)) {
                logInsert = "INSERT INTO " + utilService.getGSYY() + ".gs_yy_log(ID,methodName,\n" +
                        " ts, state, inJson, outJson, dateid, yyid, gsUrl ,yyts) VALUES " +
                        "(SYS_GUID(),'" + methodName + "',SYSDATE,'" + state + "'," + toClob + "," + toClob1 + "," +
                        "'" + dateid + "'," +
                        "'" + yyidStr + "'," +
                        "'" + gsUrl + "'," +
                        "to_date('" + TS + "', 'yyyy-mm-dd hh24:mi:ss'))";
            }
        } else {
            logInsert = "INSERT INTO " + utilService.getGSYY() + ".gs_yy_log(ID,methodName,\n" +
                    " ts, state, inJson, outJson, dateid, yyid, gsUrl ,yyts) VALUES " +
                    "(SYS_GUID(),'" + methodName + "',SYSDATE,'" + state + "'," +
                    "'" + inJson + "'," +
                    "'" + outJson + "'," +
                    "'" + dateid + "'," +
                    "'" + yyid + "'," +
                    "'" + gsUrl + "'," +
                    "to_date('" + TS + "', 'yyyy-mm-dd hh24:mi:ss'))";
        }

        int loginsert = jdbcTemplate.update(logInsert);
        return loginsert;
    }

    /**
     * 添加日志 ---项目
     *
     * @param
     * @throws
     */
    @Override
    public int loginfoItem(JdbcTemplate jdbcTemplate, String methodName, String ts, String state, Integer num, String inJson, String outJson, String dateid, String yyid, String gsUrl, String TS) {
        //字符串转义
        inJson = SafeStringUtils.returnValuetransfer(inJson);
        outJson = SafeStringUtils.returnValuetransfer(outJson);
        //SQL拼接
        String logInsert = "";
        //oracle需要使用TO_CLOB
        if (databaseType.equals("oracle")) {
            //入参
            String toClob = "";
            String[] injson = strToStrArray(inJson, 2000);
            for (int i = 0; i < injson.length; i++) {
                toClob += "TO_CLOB('" + injson[i] + "')||";
            }
            toClob += "TO_CLOB('')";
            //返回参数
            String toClob1 = "";
            String[] outjson = strToStrArray(outJson, 2000);
            for (int i = 0; i < outjson.length; i++) {
                toClob1 += "TO_CLOB('" + outjson[i] + "')||";
            }
            toClob1 += "TO_CLOB('')";
            //YYID
            String yyidStr = "";
            if (StrUtil.isNotBlank(yyid)) {
                String[] yyidjson = strToStrArray(yyid, 2000);
                for (int i = 0; i < yyidjson.length; i++) {
                    yyidStr += "TO_CLOB('" + yyidjson[i] + "')||";
                }
                yyidStr += "TO_CLOB('')";
            }
            //TO_CLOB不需要使用''
            logInsert = "INSERT INTO " + utilService.getGSYY() + ".gs_yy_log(ID,methodName, ts, state, num,inJson, outJson, dateid, yyid, gsUrl ,yyts) " +
                    "VALUES " +
                    "(SYS_GUID(),'" + methodName + "', SYSDATE, '" + state + "', '" + num + "', " + toClob + ", " + toClob1 + ", '" + dateid + "'," + yyidStr + ",'" + gsUrl + "',to_date('" + TS + "', 'yyyy-mm-dd hh24:mi:ss'))";
            //如果yyid为那么需要使用''
            if (StrUtil.isBlank(yyid)) {
                logInsert = "INSERT INTO " + utilService.getGSYY() + ".gs_yy_log(ID,methodName, ts, state, num,inJson, outJson, dateid, yyid, gsUrl ,yyts) " +
                        "VALUES " +
                        "(SYS_GUID(),'" + methodName + "', SYSDATE, '" + state + "', '" + num + "', " + toClob + ", " + toClob1 + ", '" + dateid + "','" + yyidStr + "','" + gsUrl + "',to_date('" + TS + "', 'yyyy-mm-dd hh24:mi:ss'))";

            }
        } else {
            logInsert = "INSERT INTO " + utilService.getGSYY() + ".gs_yy_log(ID,methodName, ts, state, num,inJson, outJson, dateid, yyid, gsUrl ,yyts) " +
                    "VALUES " +
                    "(SYS_GUID(),'" + methodName + "', SYSDATE, '" + state + "', '" + num + "', '" + inJson + "', '" + outJson + "', '" + dateid + "','" + yyid + "','" + gsUrl + "',to_date('" + TS + "', 'yyyy-mm-dd hh24:mi:ss'))";

        }
        int loginsert = jdbcTemplate.update(logInsert);
        return loginsert;
    }

    /*
     * 将字符串按照指定长度分割成字符串数组
     * @return
     */
    public String[] strToStrArray(String src, int length) {
        //检查参数是否合法
        if (null == src || src.equals("")) {
            return null;
        }
        if (length <= 0) {
            return null;
        }

        //获取整个字符串可以被切割成字符子串的个数
        int n = (src.length() + length - 1) / length;
        String[] split = new String[n];
        for (int i = 0; i < n; i++) {
            if (i < (n - 1)) {
                split[i] = src.substring(i * length, (i + 1) * length);
            } else {
                split[i] = src.substring(i * length);
            }
        }
        return split;
    }
}
